package com.henry.chatkit.utils

import android.annotation.SuppressLint
import android.provider.ContactsContract
import androidx.annotation.NonNull
import java.text.SimpleDateFormat
import java.util.*

object TimeUtils {

    private val minute = 60 * 1000 // 1分钟
    private val hour = 60 * minute // 1小时
    private val day = 24 * hour // 1天
    private val month = 31 * day // 月
    private val year = 12 * month // 年

    fun getShortTime(time: Long): String? {
        val str = time.toString()
        if (str.length <= 10) {
            return getShortTime(Date(time * 1000))
        }
        return getShortTime(Date(time))
    }

    fun getTime(time: Long): String? {
        val str = time.toString()
        if (str.length <= 10) {
            return getTime(Date(time * 1000))
        }
        return getTime(Date(time))
    }

    fun getShortTime(date: Date?): String? {
        if (date == null) {
            return null
        }
        val calendar = Calendar.getInstance()
        val currentDayIndex = calendar[Calendar.DAY_OF_YEAR]
        val currentYear = calendar[Calendar.YEAR]
        calendar.time = date
        val msgYear = calendar[Calendar.YEAR]
        val msgDayIndex = calendar[Calendar.DAY_OF_YEAR]
        val msgMinute = calendar[Calendar.MINUTE]
        var msgTimeStr = calendar[Calendar.HOUR_OF_DAY].toString() + ":"
        msgTimeStr = if (msgMinute < 10) {
            msgTimeStr + "0" + msgMinute
        } else {
            msgTimeStr + msgMinute
        }
        val msgDayInWeek = calendar[Calendar.DAY_OF_WEEK]
        msgTimeStr = if (currentDayIndex == msgDayIndex) {
            // 上午 下午
            date2String(date, "a HH:mm")
        } else {
            if (currentDayIndex - msgDayIndex == 1 && currentYear == msgYear) {
                date2String(date, "昨天 HH:mm")
            } else if (currentDayIndex - msgDayIndex == 2 && currentYear == msgYear) {
                date2String(date, "前天 HH:mm")
            } else if (currentDayIndex - msgDayIndex > 1 && currentYear == msgYear) { //本年消息
                //不同周显示具体月，日，注意函数：calendar.get(Calendar.MONTH) 一月对应0，十二月对应11
                // TODO 本周
                date2String(date, "M-d")
            } else { // 1、非正常时间，如currentYear < msgYear，或者currentDayIndex < msgDayIndex
                //2、非本年消息（currentYear > msgYear），如：历史消息是2018，今年是2019，显示年、月、日
                date2String(date, "yy-M-d")
            }
        }
        return msgTimeStr
    }

    /**
     * 返回文字描述的日期
     *
     * @param date
     * @return
     */
    fun getTime(date: Date?): String? {
        if (date == null) {
            return null
        }
        val calendar = Calendar.getInstance()
        val currentDayIndex = calendar[Calendar.DAY_OF_YEAR]
        val currentYear = calendar[Calendar.YEAR]
        calendar.time = date
        val msgYear = calendar[Calendar.YEAR]
        val msgDayIndex = calendar[Calendar.DAY_OF_YEAR]
        val msgMinute = calendar[Calendar.MINUTE]
        val msgDayInWeek = calendar[Calendar.DAY_OF_WEEK]
        return if (currentDayIndex == msgDayIndex) {
            date2String(date, "HH:mm")
        } else {
            if (currentDayIndex - msgDayIndex == 1 && currentYear == msgYear) {
                date2String(date, "昨天 HH:mm")
            } else if (currentDayIndex - msgDayIndex == 2 && currentYear == msgYear) {
                date2String(date, "前天 HH:mm")
            } else if (currentDayIndex - msgDayIndex > 1 && currentYear == msgYear) { //本年消息
                // TODO 本周
                //不同周显示具体月，日，注意函数：calendar.get(Calendar.MONTH) 一月对应0，十二月对应11
                date2String(date, "MM-dd HH:mm")
            } else { // 1、非正常时间，如currentYear < msgYear，或者currentDayIndex < msgDayIndex
                //2、非本年消息（currentYear > msgYear），如：历史消息是2018，今年是2019，显示年、月、日
                date2String(date, "yyyy-MM-dd HH:mm")
            }
        }
    }

    fun date2String(date: Date, @NonNull pattern: String): String {
        return getSafeDateFormat(pattern).format(date)
    }


    // 参考 utilcode
    private val SDF_THREAD_LOCAL: ThreadLocal<MutableMap<String?, SimpleDateFormat?>> =
        object : ThreadLocal<MutableMap<String?, SimpleDateFormat?>>() {
            override fun initialValue(): MutableMap<String?, SimpleDateFormat?> {
                return HashMap()
            }
        }

    @SuppressLint("SimpleDateFormat")
    fun getSafeDateFormat(pattern: String): SimpleDateFormat {
        val sdfMap: MutableMap<String?, SimpleDateFormat?>? = SDF_THREAD_LOCAL.get()
        //noinspection ConstantConditions
        var simpleDateFormat = sdfMap?.get(pattern)
        if (simpleDateFormat == null) {
            simpleDateFormat = SimpleDateFormat(pattern)
            sdfMap?.set(pattern, simpleDateFormat)
        }
        return simpleDateFormat;

    }

}